***creating occ weights by industry year 

clear



// Load Data
use "$dir/data/data_public/census_1970_2000.dta" , clear
	
	
	// Prepare
	
keep if year == 1970 | year == 1980 | year == 1990 | year == 2000
	
	keep if age >=15 & age<65
	
	*empstat==1 means employed
	keep if empstat == 1
	
	*want to exclude self-employed CLASSWKR == 1
	keep if classwkr != 1 
	
	
	*want to drop if not a valid occ2010 occupation
	
	keep if occ2010 != .
	
	*want to drop if not a valid industry
	
	keep if ind1990 != .
	
	
	
	
	
	sort occ2010
	
	keep year occ2010 ind1990 perwt
	
	*want to create new occupation category for non-OAS
	
	
	gen OAS_occ_cat = 0 //if nonoas, get category 0
	replace OAS_occ_cat = occ2010 if  (occ2010 >=5000 & occ2010 <6000)

	foreach i in 0 5000 5010 5020 5030 5100 5110 5120 5130 5140 5150 5160 5165 5200 ///
	5220 5230 5240 5250 5260 5300 5310 5320 5330 5340 5350 5360 5400 5410 5420 5500 ///
	5510 5520 5530 5540 5550 5560 5600 5610 5620 5630 5700 5800 5810 5820 5840 5850 ///
	5860 5900 5910 5920 5940 {
	
gen occ_`i' = 0
replace occ_`i' = 1 if occ2010 == `i' 
} 

  
replace occ_0 = 1 if (occ2010 <5000 | occ2010 >=6000)
	
	
	collapse (sum) occ_* [aweight= perwt], by(year ind1990)
	
	gen nonoas=occ_0
	
	*want to reshape this long
	
	*
	reshape long occ_, i(year ind1990)
	
	rename _j occ2010
	
	by ind1990 year, sort: egen total             = total(occ_)
	

	
	gen occ_share = occ_/total
	gen occ_share_oas = occ_/(total-nonoas)


*creates the occupation distrubtion by industry each year
save "$dir/data/interim/occ_by_ind_weights_by_year.dta" , replace

	
	
	
	*creating industry by comuting zone year weights for years after 2000
	
	
	clear




// Load Data

use "$dir/data/data_public/acs_bgyears.dta", clear



	keep if age >=15 & age<65
	
	
	*labforce ==2 -> in the labor force
	

	
		*want to exclude self-employed CLASSWKR == 1
	
	gen emp = (empstat == 1) if classwkr != 1 
	
	
	*empstat==1 means employed
	
	keep if emp == 1
	

	
	// Commuting Zone

				gen     puma2000 = real(string(statefip) + "00" + string(puma)) if puma<1000 
		replace puma2000 = real(string(statefip) + "0" + string(puma)) if puma>=1000 & puma<10000
		replace puma2000 = real(string(statefip) + string(puma)) if puma>=10000
		
		joinby puma2000 using "$dir/data/interim/cw_cty_czone_puma2000_afact.dta" , unmatched(master) 
				drop _merge
				
		gen new_weight = perwt*afact
		
	

	collapse (sum) emp  [pw=new_weight], by(year ind1990 czone)
	

by czone year, sort: egen total_emp              = total(emp)

gen ind_share = emp/total_emp


		keep year czone ind1990 ind_share
	

	
		save "$dir/data/interim/ind_cz_weights_2007_2016.dta", replace

		
		
*creating 1970 weights
	
	
	clear



// Load Data

use "$dir/data/data_public/census_1970_2000.dta" , clear

keep if year == 1970

	keep if age >=15 & age<65
	
	
	*labforce ==2 -> in the labor force
	

	
		*want to exclude self-employed CLASSWKR == 1
	
	gen emp = (empstat == 1) if classwkr != 1 
	
	
	*empstat==1 means employed
	
	keep if emp == 1
	

	
	// Commuting Zone

				
			
		rename cntygp97 cty_grp70

		
		 joinby cty_grp70 using "$dir/data/data_public/cw_ctygrp1970_czone_corr.dta" , unmatched(master) 
				
		gen new_weight = perwt*afact
		
	

	collapse (sum) emp  [pw=new_weight], by(year ind1990 czone)
	

by czone year, sort: egen total_emp              = total(emp)

gen ind_share = emp/total_emp


		keep year czone ind1990 ind_share
	
		save "$dir/data/interim/ind_cz_weights_1970.dta", replace
		
		
		*creating industry by comuting zone year weights for year 2000
	
	
	clear





// Load Data


use "$dir/data/data_public/census_1970_2000.dta" , clear
keep if year == 2000

	keep if age >=15 & age<65
	
	
	*labforce ==2 -> in the labor force
	

	
		*want to exclude self-employed CLASSWKR == 1
	
	gen emp = (empstat == 1) if classwkr != 1 
	
	
	*empstat==1 means employed
	
	keep if emp == 1
	

	
	// Commuting Zone

				gen     puma2000 = real(string(statefip) + "00" + string(puma)) if puma<1000 
		replace puma2000 = real(string(statefip) + "0" + string(puma)) if puma>=1000 & puma<10000
		replace puma2000 = real(string(statefip) + string(puma)) if puma>=10000
		

		joinby puma2000 using "$dir/data/interim/cw_cty_czone_puma2000_afact.dta" , unmatched(master) 
				drop _merge
				
		gen new_weight = perwt*afact
		

	

	collapse (sum) emp  [pw=new_weight], by(year ind1990 czone)
	

by czone year, sort: egen total_emp              = total(emp)

gen ind_share = emp/total_emp


		keep year czone ind1990 ind_share
	

	
		save "$dir/data/interim/ind_cz_weights_2000.dta", replace
